from django.shortcuts import render
from rest_framework.views import  APIView
from rest_framework.response import Response
from course import models,courseser
import json
# Create your views here.
class HomeCate(APIView):
    def get(self,request):
        allcate=models.Category.objects.filter(parent=None,flag=True).order_by("order")
        ser = courseser.CategorySerializer(allcate, many=True)
        data=ser.data
        for item in data:
            item["children"]= item["children"][:3]
        return Response({"code":200,"msg":"ok","data":data})

#首页展示二级分类的接口
class HomeChild(APIView):
    def get(self,request):
        #获取到一级分类的参数
        cate=request.query_params.get("cate","")
        if not cate:
            return Response("参数不对")
        #根据一级分类的
        catelist=models.Category.objects.get(id=cate,flag=True)
        ser=courseser.CategorySerializer(catelist)
        data=ser.data
        data["course"]=[]
        return Response({"code":200,"msg":"ok","data":data})